package com.qingcloud.adminbackend.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingcloud.adminbackend.entity.config.SysSensorFactorConfig;
import com.qingcloud.adminbackend.entity.product.SysProductType;
import com.qingcloud.adminbackend.entity.sensor.SysSensorFactorConfigDTO;
import com.qingcloud.adminbackend.mapper.SysProductTypeMapper;
import com.qingcloud.adminbackend.mapper.SysSensorFactorConfigMapper;
import com.qingcloud.adminbackend.service.ISysSensorFactorConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 传感器监测因子配置数据表 服务实现类
 * </p>
 *
 * @author luozehuas
 * @since 2019-04-12
 */
@Service
public class SysSensorFactorConfigServiceImpl extends ServiceImpl<SysSensorFactorConfigMapper, SysSensorFactorConfig> implements ISysSensorFactorConfigService {

    @Resource
    private SysSensorFactorConfigMapper sysSensorFactorConfigMapper;

    @Resource
    private SysProductTypeMapper productTypeMapper;

    /**
     * 具体监测因子
     *
     * @return
     */
    @Override
    public List<SysSensorFactorConfigDTO> getSensorFactor() {
        return sysSensorFactorConfigMapper.getSensorFactor();
    }

    @Override
    public List<SysSensorFactorConfigDTO> findByOrderType(Integer orderType) {
        return sysSensorFactorConfigMapper.findByOrderType(orderType);
    }

    @Override
    public List<SysSensorFactorConfigDTO> findAlineFactor(Integer businessType) {
        return sysSensorFactorConfigMapper.findAlineFactor(businessType);
    }

    @Override
    public List<SysSensorFactorConfigDTO> findVocsFactor(Integer businessType) {
        return sysSensorFactorConfigMapper.findVocsFactor(businessType);
    }
    /**
     * 根据产品类型查询可用监测因子
     *
     * @param productType
     * @return
     */
    @Override
    public List<SysSensorFactorConfigDTO> findSensorFactorByProductType(String productType) {
        QueryWrapper<SysProductType> query = new QueryWrapper<>();
        query.eq("product_type", productType);
        SysProductType sysProductType = productTypeMapper.selectOne(query);
        if (sysProductType == null) {
            return Collections.emptyList();
        }
        //可用因子
        String sensorFactors = sysProductType.getSensorFactors();
        if (StrUtil.isBlank(sensorFactors)) {
            return Collections.emptyList();
        }
        List<String> factorIds = Arrays.asList(sensorFactors.split(","));
        return baseMapper.findByIds(factorIds);
    }

    @Override
    public List<SysSensorFactorConfigDTO> getDefaultSensorFactor() {
        return sysSensorFactorConfigMapper.getDefaultSensorFactor();
    }
}
